/**
 * @file mmath.c
 * @author your name (you@domain.com)
 * @brief
 * @version false.true
 * @date 2025-05-06
 *
 * @copyright Copyright (c) 2025
 *
 */

#include "mmath.h"
#include "delay.h"
#include "xmzTypeDefine.h"
#include <float.h>
#include <math.h>
#include <stdint.h>

#include <stdarg.h>

//////////////////梯形图
// CALCULATE ❌
// ADD 🎈
// SUB 🎈
// MUL 🎈
// DIV 🎈
// MOD 🎈
// NEG
// INC 🎈
// DEC 🎈
// ABS 🎈
/// MIN
/// MAX
/// LIMIT
// SQR
// SQRT
// LN
// EXP
// SIN
// COS
// TAN
// ASIN
// ACOS
// ATAN
// FRAC
// EXPT
///////////////////ST
// ABS
/// MIN
/// MAX
/// LIMIT
// SQR
// SQRT
// LN
// EXP
// SIN
// COS
// TAN
// ASIN
// ACOS
// ATAN
// FRAC

/////////////////

// #region 加法
// Int(int16_t) 🎈
Bool Add_Int(Bool en, Int* out, int Count, ...) {
  if(!en || !out || Count <= 0)
    return false;
  va_list args;
  va_start(args, Count);
  int16_t sum = 0;
  for(int i = 0; i < Count; i++) {
    // 参数传入时自动提升为 int
    int16_t val = (int16_t) va_arg(args, int);
    sum += val;
  }
  va_end(args);
  *out = (Int) sum;
  return en;
}

// SInt (int8_t)🎈
Bool Add_SInt(Bool en, SInt* out, int Count, ...) {
  if(!en || !out || Count <= 0)
    return false;
  va_list args;
  va_start(args, Count);
  int8_t sum = 0;
  for(int i = 0; i < Count; i++) {
    // int8_t 在可变参数中会提升为 int
    int8_t val = (int8_t) va_arg(args, int);
    sum += val;
  }
  va_end(args);
  *out = (SInt) sum;
  return en;
}

// USInt (uint8_t)🎈
Bool Add_USInt(Bool en, USInt* out, int Count, ...) {
  if(!en || !out || Count <= 0)
    return false;
  va_list args;
  va_start(args, Count);
  uint8_t sum = 0;
  for(int i = 0; i < Count; i++) {
    uint8_t val = (uint8_t) va_arg(args, int); // uint8_t promoted to int
    sum += val;
  }
  va_end(args);
  *out = (USInt) sum;
  return en;
}

// UInt (uint16_t) 🎈
Bool Add_UInt(Bool en, UInt* out, int Count, ...) {
  if(!en || !out || Count <= 0)
    return false;
  va_list args;
  va_start(args, Count);
  uint16_t sum = 0;
  for(int i = 0; i < Count; i++) {
    uint16_t val = (uint16_t) va_arg(args, int);
    sum += val;
  }
  va_end(args);
  *out = (UInt) sum;
  return en;
}

// DInt (int32_t) 🎈
Bool Add_DInt(Bool en, DInt* out, int Count, ...) {
  if(!en || !out || Count <= 0)
    return false;
  va_list args;
  va_start(args, Count);
  int32_t sum = 0;
  for(int i = 0; i < Count; i++) {
    int32_t val = va_arg(args, int32_t);
    sum += val;
  }
  va_end(args);
  *out = (DInt) sum;
  return en;
}

// UDInt (uint32_t) 🎈
Bool Add_UDInt(Bool en, UDInt* out, int Count, ...) {
  if(!en || !out || Count <= 0)
    return false;
  va_list args;
  va_start(args, Count);
  UDInt sum = 0;
  for(int i = 0; i < Count; i++) {
    UDInt val = va_arg(args, UDInt);
    sum += val;
  }
  va_end(args);
  *out = (UDInt) sum;
  return en;
}

// Real (float) 🎈
Bool Add_Real(Bool en, Real* out, int Count, ...) {
  if(!en || !out || Count <= 0)
    return false;
  va_list args;
  va_start(args, Count);
  Real sum = 0.0f;
  for(int i = 0; i < Count; i++) {
    Real val = (Real) va_arg(args, double);
    sum += (Real) val;
  }
  va_end(args);
  *out = sum;
  return en;
}

// LReal (double) 🎈
Bool Add_LReal(Bool en, LReal* out, int Count, ...) {
  if(!en || !out || Count <= 0)
    return false;
  va_list args;
  va_start(args, Count);
  LReal sum = 0.0;
  for(int i = 0; i < Count; i++) {
    LReal val = va_arg(args, LReal);
    sum += val;
  }
  va_end(args);
  *out = sum;
  return en;
}
Bool Add_Time(Bool en, Time* out, int Count, ...) {
  if(!en || !out || Count <= 0)
    return false;
  va_list args;
  va_start(args, Count);
  int32_t sum = 0;
  for(int i = 0; i < Count; i++) {
    int32_t val = va_arg(args, int32_t);
    sum += val;
  }
  va_end(args);
  *out = (DInt) sum;
  return en;
}

// #endregion

// #region 乘法
// Int (int16_t) 🎈
Bool Mul_Int(Bool en, int16_t* out, int Count, ...) {
  if(!en || !out || Count <= 0)
    return false;
  va_list args;
  va_start(args, Count);
  int64_t product = 1; // 64为存储中间过程数据,避免过早溢出
  for(int i = 0; i < Count; i++) {
    int16_t val = (int16_t) va_arg(args, int);
    product *= val;
  }
  va_end(args);
  *out = (int16_t) product;
  return en;
}

// SInt (int8_t) 🎈
Bool Mul_SInt(Bool en, int8_t* out, int Count, ...) {
  if(!en || !out || Count <= 0)
    return false;
  va_list args;
  va_start(args, Count);
  int64_t product = 1; // 64为存储中间过程数据,避免过早溢出
  for(int i = 0; i < Count; i++) {
    int8_t val = (int8_t) va_arg(args, int); // 提升，只做隐式转换
    product *= val;
  }
  va_end(args);
  *out = (int8_t) product;
  return en;
}
// DInt (int32_t) 🎈
Bool Mul_DInt(Bool en, int32_t* out, int Count, ...) {
  if(!en || !out || Count <= 0)
    return false;
  va_list args;
  va_start(args, Count);
  int64_t product = 1;
  for(int i = 0; i < Count; i++) {
    int32_t val = va_arg(args, int32_t);
    product *= val;
  }
  va_end(args);
  *out = (int32_t) product;
  return en;
}
// USInt (uint8_t) 🎈
Bool Mul_USInt(Bool en, uint8_t* out, int Count, ...) {
  if(!en || !out || Count <= 0)
    return false;
  va_list args;
  va_start(args, Count);
  uint64_t product = 1;
  for(int i = 0; i < Count; i++) {
    uint8_t val = (uint8_t) va_arg(args, int); // 提升，只做隐式转换
    product *= val;
  }
  *out = (uint8_t) product;
  return en;
}
// UInt (uint16_t) 🎈
Bool Mul_UInt(Bool en, uint16_t* out, int Count, ...) {
  if(!en || !out || Count <= 0)
    return false;
  va_list args;
  va_start(args, Count);
  uint64_t product = 1;
  for(int i = 0; i < Count; i++) {
    uint16_t val = (uint16_t) va_arg(args, int);
    product *= val;
  }
  va_end(args);
  *out = (uint16_t) product;
  return en;
}

// UDInt (uint32_t) 🎈
Bool Mul_UDInt(Bool en, UDInt* out, int Count, ...) {
  if(!en || !out || Count <= 0)
    return false;
  va_list args;
  va_start(args, Count);
  uint64_t product = 1;
  for(int i = 0; i < Count; i++) {
    UDInt val = va_arg(args, UDInt);
    product *= val;
  }
  va_end(args);
  *out = (UDInt) product;
  return en;
}

// Real (float) 🎈
Bool Mul_Real(Bool en, float* out, int Count, ...) {
  if(!en || !out || Count <= 0)
    return false;
  va_list args;
  va_start(args, Count);

  double product = 1.0;
  for(int i = 0; i < Count; i++) {
    float val = (float) va_arg(args, double); // 不提升，只做隐式转换
    product *= (float) val;
  }
  va_end(args);
  *out = (Real) product;
  return en;
}

// LReal (double) 🎈
Bool Mul_LReal(Bool en, double* out, int Count, ...) {
  if(!en || !out || Count <= 0)
    return false;
  va_list args;
  va_start(args, Count);

  double product = 1.0;
  for(int i = 0; i < Count; i++) {
    double val = va_arg(args, double);
    product *= val;
  }
  va_end(args);
  *out = (LReal) product;
  return en;
}

// #endregion

// SInt 减法
Bool Sub_SInt(Bool EN, SInt* out, SInt in1, SInt in2) {
  if(!EN || !out || isnan(*out) || isnan(in1) || isnan(in2))
    return false;
  int64_t result = (int64_t) in1 - in2;

  *out = (SInt) result;
  return true;
}

// USInt 减法
Bool Sub_USInt(Bool EN, USInt* out, USInt in1, USInt in2) {
  if(!EN || !out || isnan(*out) || isnan(in1) || isnan(in2))
    return false;
  int64_t result = (int64_t) in1 - in2; // 使用 int64_t 避免负数

  *out = (USInt) result;
  return true;
}

// Int 减法
Bool Sub_Int(Bool EN, Int* out, Int in1, Int in2) {
  if(!EN || !out || isnan(*out) || isnan(in1) || isnan(in2))
    return false;

  int64_t result = (int64_t) in1 - in2;

  *out = (Int) result;
  return true;
}

// UInt 减法
Bool Sub_UInt(Bool EN, UInt* out, UInt in1, UInt in2) {
  if(!EN || !out || isnan(*out) || isnan(in1) || isnan(in2))
    return false;
  int64_t result = (int64_t) in1 - in2;

  *out = (UInt) result;
  return true;
}

// DInt 减法
Bool Sub_DInt(Bool EN, DInt* out, DInt in1, DInt in2) {
  if(!EN || !out || isnan(*out) || isnan(in1) || isnan(in2))
    return false;
  int64_t result = (int64_t) in1 - in2;

  *out = (DInt) result;
  return true;
}

// UDInt 减法
Bool Sub_UDInt(Bool EN, UDInt* out, UDInt in1, UDInt in2) {
  if(!EN || !out || isnan(*out) || isnan(in1) || isnan(in2))
    return false;
  int64_t result = (int64_t) in1 - in2;
  *out           = (UDInt) result;
  return true;
}

// Real 减法
Bool Sub_Real(Bool EN, Real* out, Real in1, Real in2) {
  if(!EN || !out || isnan(*out) || isnan(in1) || isnan(in2))
    return false;
  Real result = in1 - in2;

  *out = result;
  return true;
}

// LReal 减法
Bool Sub_LReal(Bool EN, LReal* out, LReal in1, LReal in2) {
  if(!EN || !out || isnan(*out) || isnan(in1) || isnan(in2))
    return false;
  LReal result = in1 - in2;

  *out = result;
  return true;
}

// SInt 除法
Bool Div_SInt(Bool EN, SInt* out, SInt in1, SInt in2) {
  if(!EN || !out || isnan(*out) || isnan(in1) || isnan(in2) || in2 == 0)
    return false;

  int64_t result = (int64_t) in1 / in2;

  *out = (SInt) result;
  return true;
}

// USInt 除法
Bool Div_USInt(Bool EN, USInt* out, USInt in1, USInt in2) {
  if(!EN || !out || isnan(*out) || isnan(in1) || isnan(in2) || in2 == 0)
    return false;
  uint64_t result = (uint64_t) in1 / in2;

  *out = (USInt) result;
  return true;
}

// Int 除法
Bool Div_Int(Bool EN, Int* out, Int in1, Int in2) {
  if(!EN || !out || isnan(*out) || isnan(in1) || isnan(in2) || in2 == 0)
    return false;
  int64_t result = (int64_t) in1 / in2;

  *out = (Int) result;
  return true;
}

// UInt 除法
Bool Div_UInt(Bool EN, UInt* out, UInt in1, UInt in2) {
  if(!EN || !out || isnan(*out) || isnan(in1) || isnan(in2) || in2 == 0)
    return false;

  uint64_t result = (uint64_t) in1 / in2;

  *out = (UInt) result;
  return true;
}

// DInt 除法
Bool Div_DInt(Bool EN, DInt* out, DInt in1, DInt in2) {
  if(!EN || !out || isnan(*out) || isnan(in1) || isnan(in2) || in2 == 0)
    return false;

  int64_t result = (int64_t) in1 / in2;

  *out = (DInt) result;
  return true;
}

// UDInt 除法
Bool Div_UDInt(Bool EN, UDInt* out, UDInt in1, UDInt in2) {
  if(!EN || !out || isnan(*out) || isnan(in1) || isnan(in2) || in2 == 0)
    return false;

  uint64_t result = (uint64_t) in1 / in2;

  *out = (UDInt) result;
  return true;
}

// Real 除法
Bool Div_Real(Bool EN, Real* out, Real in1, Real in2) {
  if(!EN || !out || isnan(*out) || isnan(in1) || isnan(in2) || in2 == 0)
    return false;

  Real result = in1 / in2;

  *out = result;
  return true;
}

// LReal 除法
Bool Div_LReal(Bool EN, LReal* out, LReal in1, LReal in2) {
  if(!EN || isnan(in1) || isnan(in2))
    return false;
  if(!out)
    return false;
  if(in2 == 0.0)
    return false; // 除数为0

  LReal result = (LReal) (in1 / in2);
  *out         = result;
  return true;
}

// DInt 除法
Bool Div_Time(Bool EN, Time* out, Time in1, Time in2) {
  if(!EN || !out || isnan(*out) || isnan(in1) || isnan(in2) || in2 == 0)
    return false;

  int64_t result = (int64_t) in1 / in2;

  *out = (Time) result;
  return true;
}

// SInt 取余
Bool Mod_SInt(Bool EN, SInt* out, SInt in1, SInt in2) {
  if(!EN || isnan(in1) || isnan(in2))
    return 0; // EN 为 false
  if(!out)
    return 0; // 无效输出指针
  if(in2 == 0)
    return 0; // 除数为0

  SInt result = in1 % in2;
  // 结果应在 [-127, 127]，C 的 % 保证结果在范围内，无需额外检查
  *out = result;
  return 1;
}

// USInt 取余
Bool Mod_USInt(Bool EN, USInt* out, USInt in1, USInt in2) {
  if(!EN || isnan(in1) || isnan(in2))
    return 0;
  if(!out)
    return 0;
  if(in2 == 0)
    return 0; // 除数为0

  USInt result = in1 % in2;
  // 结果应在 [0, 255]，C 的 % 保证结果在范围内
  *out = result;
  return 1;
}

// Int 取余
Bool Mod_Int(Bool EN, Int* out, Int in1, Int in2) {
  if(!EN || isnan(in1) || isnan(in2))
    return 0;
  if(!out)
    return 0;
  if(in2 == 0)
    return 0; // 除数为0

  Int result = in1 % in2;
  // 结果应在 [-32767, 32767]，C 的 % 保证结果在范围内
  *out = result;
  return 1;
}

// UInt 取余 uint16_t
Bool Mod_UInt(Bool EN, UInt* out, UInt in1, UInt in2) {
  if(!EN || isnan(in1) || isnan(in2))
    return 0;
  if(!out)
    return 0;
  if(in2 == 0)
    return 0; // 除数为0

  UInt result = in1 % in2;
  // 结果应在 [0, 65535]，C 的 % 保证结果在范围内
  *out = result;
  return 1;
}

// DInt 取余
Bool Mod_DInt(Bool EN, DInt* out, DInt in1, DInt in2) {
  if(!EN || isnan(in1) || isnan(in2))
    return 0;
  if(!out)
    return 0;
  if(in2 == 0)
    return 0; // 除数为0

  DInt result = in1 % in2;
  // 结果应在 [-2147483647, 2147483647]，C 的 % 保证结果在范围内
  *out = result;
  return 1;
}

// UDInt 取余  uint32_t
Bool Mod_UDInt(Bool EN, UDInt* out, UDInt in1, UDInt in2) {
  if(!EN || isnan(in1) || isnan(in2))
    return 0;
  if(!out)
    return 0;
  if(in2 == 0)
    return 0; // 除数为0

  UDInt result = in1 % in2;
  // 结果应在 [0, 4294967295]，C 的 % 保证结果在范围内
  *out = result;
  return 1;
}
// 取负函数声明
uint8_t Neg_SInt(uint8_t EN, SInt* operand) {
  if(!EN || !operand || isnan(*operand))
    return 0;

  if(*operand > 0)
    *operand = -*operand;

  return 1; // 无论是否取负，处理都是成功的
}
uint8_t Neg_Int(uint8_t EN, Int* operand) {
  if(!EN || !operand || isnan(*operand))
    return 0;

  if(*operand > 0)
    *operand = -*operand;

  return 1;
}
uint8_t Neg_DInt(uint8_t EN, DInt* operand) {
  if(!EN || !operand || isnan(*operand))
    return 0;

  if(*operand > 0)
    *operand = -*operand;

  return 1;
}
uint8_t Neg_Real(uint8_t EN, Real* operand) {
  if(!EN || !operand || isnan(*operand))
    return 0;

  if(*operand > 0.0f)
    *operand = -*operand;

  return 1;
}
uint8_t Neg_LReal(uint8_t EN, LReal* operand) {
  if(!EN || !operand || isnan(*operand))
    return 0;

  if(*operand > 0.0)
    *operand = -*operand;

  return 1;
}

// SInt 递增
Bool Inc_SInt(Bool EN, SInt* operand) {
  if(!EN || !operand || isnan(*operand))
    return 0; // EN 为 false
  if(!operand)
    return 0; // 无效指针

  *operand += 1; // 允许溢出（如 127 -> -128）
  return 1;
}

// USInt 递增
Bool Inc_USInt(Bool EN, USInt* operand) {
  if(!EN || !operand || isnan(*operand))
    return 0;
  if(!operand)
    return 0;

  *operand += 1; // 允许溢出（如 255 -> 0）
  return 1;
}

// Int 递增
Bool Inc_Int(Bool EN, Int* operand) {
  if(!EN || !operand || isnan(*operand))
    return 0;
  *operand += 1; // 允许溢出（如 32767 -> -32768）
  return 1;
}

// UInt 递增
Bool Inc_UInt(Bool EN, UInt* operand) {
  if(!EN || !operand || isnan(*operand))
    return 0;
  *operand += 1; // 允许溢出（如 65535 -> 0）
  return 1;
}

// DInt 递增
Bool Inc_DInt(Bool EN, DInt* operand) {
  if(!EN || !operand || isnan(*operand))
    return 0;
  *operand += 1; // 允许溢出（如 2147483647 -> -2147483648）
  return 1;
}

// UDInt 递增
Bool Inc_UDInt(Bool EN, UDInt* operand) {
  if(!EN || !operand || isnan(*operand))
    return 0;
  *operand += 1; // 允许溢出（如 4294967295 -> 0）
  return 1;
}

// SInt 递减
Bool Dec_SInt(Bool EN, SInt* operand) {
  if(!EN || !operand || isnan(*operand))
    return 0;
  *operand -= 1; // 允许溢出（如 -128 -> 127）
  return 1;
}

// USInt 递减
Bool Dec_USInt(Bool EN, USInt* operand) {
  if(!EN || !operand || isnan(*operand))
    return 0;
  *operand -= 1; // 允许溢出（如 0 -> 255）
  return 1;
}

// Int 递减
Bool Dec_Int(Bool EN, Int* operand) {
  if(!EN || !operand || isnan(*operand))
    return 0;
  *operand -= 1; // 允许溢出（如 -32768 -> 32767）
  return 1;
}

// UInt 递减
Bool Dec_UInt(Bool EN, UInt* operand) {
  if(!EN || !operand || isnan(*operand))
    return 0;
  *operand -= 1; // 允许溢出（如 0 -> 65535）
  return 1;
}

// DInt 递减
Bool Dec_DInt(Bool EN, DInt* operand) {
  if(!EN || !operand || isnan(*operand))
    return 0;
  *operand -= 1; // 允许溢出（如 -2147483648 -> 2147483647）
  return 1;
}

// UDInt 递减
Bool Dec_UDInt(Bool EN, UDInt* operand) {
  if(!EN || !operand || isnan(*operand))
    return 0;
  *operand -= 1; // 允许溢出（如 0 -> 4294967295）
  return 1;
}

// 取绝对值函数声明
uint8_t ABS_SInt(uint8_t EN, SInt* out, SInt in) {
  if(!EN || !out || isnan(*out) || isnan(in))
    return 0;
  *out = (in < 0 ? -in : in); // 转换回int8_t（可能截断） -128越界了
  return 1;                   // 执行成功
}
uint8_t ABS_Int(uint8_t EN, Int* out, Int in) {
  if(!EN || !out || isnan(*out) || isnan(in))
    return 0;
  *out = (in < 0 ? -in : in);
  return 1;
}
uint8_t ABS_DInt(uint8_t EN, DInt* out, DInt in) {
  if(!EN || !out || isnan(*out) || isnan(in))
    return 0;
  *out = (in < 0) ? -in : in;
  return 1;
}
uint8_t ABS_Real(uint8_t EN, Real* out, Real in) {
  if(!EN || !out || isnan(*out) || isnan(in))
    return 0;
  *out = (in < 0.0f) ? -in : in;
  return 1;
}
uint8_t ABS_LReal(uint8_t EN, LReal* out, LReal in) {
  if(!EN || !out || isnan(*out) || isnan(in))
    return 0;
  *out = (in < 0.0) ? -in : in;
  // 或者 *out = fabs(in);
  return 1;
}
/**
 * @brief 比较两个DTL的大小
 *
 * @param in1
 * @param in2
 * @return int8_t -1：in1 < in2, 0：in1 == in2, 1：in1 > in2
 */
int8_t compare_DTL(DTL* in1, DTL* in2) {
  if(in1->YEAR != in2->YEAR)
    return (in1->YEAR < in2->YEAR) ? -1 : 1;
  if(in1->MONTH != in2->MONTH)
    return (in1->MONTH < in2->MONTH) ? -1 : 1;
  if(in1->DAY != in2->DAY)
    return (in1->DAY < in2->DAY) ? -1 : 1;
  if(in1->HOUR != in2->HOUR)
    return (in1->HOUR < in2->HOUR) ? -1 : 1;
  if(in1->MINUTE != in2->MINUTE)
    return (in1->MINUTE < in2->MINUTE) ? -1 : 1;
  if(in1->SECOND != in2->SECOND)
    return (in1->SECOND < in2->SECOND) ? -1 : 1;
  if(in1->MILLISECOND != in2->MILLISECOND)
    return (in1->MILLISECOND < in2->MILLISECOND) ? -1 : 1;
  return 0; // 相等
}
// 最大值
uint8_t MAX_SInt(uint8_t EN, SInt* out, int Count, ...) {
  if(!EN || !out || isnan(*out) || Count <= 0)
    return 0;

  va_list args;
  va_start(args, Count);

  SInt maxVal = (SInt) va_arg(args, int); // SInt会提升为int传递
  for(int i = 1; i < Count; ++i) {
    SInt val = (SInt) va_arg(args, int); // 取出并强制转换
    if(val > maxVal)
      maxVal = val;
  }

  va_end(args);

  *out = maxVal;
  return 1;
}
uint8_t MAX_Int(uint8_t EN, Int* out, int Count, ...) {
  if(!EN || !out || isnan(*out) || Count <= 0)
    return 0;
  va_list args;
  va_start(args, Count);
  Int maxVal = (Int) va_arg(args, int);
  for(int i = 1; i < Count; ++i) {
    Int val = (Int) va_arg(args, int);
    if(val > maxVal)
      maxVal = val;
  }
  va_end(args);
  *out = maxVal;
  return 1;
}
uint8_t MAX_DInt(uint8_t EN, DInt* out, int Count, ...) {
  if(!EN || !out || isnan(*out) || Count <= 0)
    return 0;
  va_list args;
  va_start(args, Count);
  DInt maxVal = (DInt) va_arg(args, int32_t);
  for(int i = 1; i < Count; ++i) {
    DInt val = (DInt) va_arg(args, int32_t);
    if(val > maxVal)
      maxVal = val;
  }
  va_end(args);
  *out = maxVal;
  return 1;
}
uint8_t MAX_USInt(uint8_t EN, USInt* out, int Count, ...) {
  if(!EN || !out || isnan(*out) || Count <= 0)
    return 0;
  va_list args;
  va_start(args, Count);
  USInt maxVal = (USInt) va_arg(args, int); // USInt提升为int
  for(int i = 1; i < Count; ++i) {
    USInt val = (USInt) va_arg(args, int);
    if(val > maxVal)
      maxVal = val;
  }
  va_end(args);
  *out = maxVal;
  return 1;
}
uint8_t MAX_UInt(uint8_t EN, UInt* out, int Count, ...) {
  if(!EN || !out || isnan(*out) || Count <= 0)
    return 0;
  va_list args;
  va_start(args, Count);
  UInt maxVal = (UInt) va_arg(args, int); // UInt提升为int
  for(int i = 1; i < Count; ++i) {
    UInt val = (UInt) va_arg(args, int);
    if(val > maxVal)
      maxVal = val;
  }
  va_end(args);
  *out = maxVal;
  return 1;
}
uint8_t MAX_UDInt(uint8_t EN, UDInt* out, int Count, ...) {
  if(!EN || !out || isnan(*out) || Count <= 0)
    return 0;
  va_list args;
  va_start(args, Count);
  UDInt maxVal = (UDInt) va_arg(args, uint32_t);
  for(int i = 1; i < Count; ++i) {
    UDInt val = (UDInt) va_arg(args, uint32_t);
    if(val > maxVal)
      maxVal = val;
  }
  va_end(args);
  *out = maxVal;
  return 1;
}
uint8_t MAX_Real(uint8_t EN, Real* out, int Count, ...) {
  if(!EN || !out || isnan(*out) || Count <= 0)
    return 0;
  va_list args;
  va_start(args, Count);
  Real maxVal = (Real) va_arg(args, double); // float提升为double
  for(int i = 1; i < Count; ++i) {
    Real val = (Real) va_arg(args, double);
    if(val > maxVal)
      maxVal = val;
  }
  va_end(args);
  *out = maxVal;
  return 1;
}
uint8_t MAX_LReal(uint8_t EN, LReal* out, int Count, ...) {
  if(!EN || !out || isnan(*out) || Count <= 0)
    return 0;
  va_list args;
  va_start(args, Count);
  LReal maxVal = va_arg(args, double);
  for(int i = 1; i < Count; ++i) {
    LReal val = va_arg(args, double);
    if(val > maxVal)
      maxVal = val;
  }
  va_end(args);
  *out = maxVal;
  return 1;
}
uint8_t MAX_DTL(uint8_t EN, DTL* out, int Count, ...) {
  if(!EN || !out || Count <= 0)
    return 0;
  va_list args;
  va_start(args, Count);
  DTL maxVal = va_arg(args, DTL); // 直接传递结构体
  for(int i = 1; i < Count; ++i) {
    DTL val = va_arg(args, DTL);       // 直接传递结构体
    if(compare_DTL(&val, &maxVal) > 0) // 使用比较函数
      maxVal = val;
  }
  va_end(args);
  *out = maxVal;
  return 1;
}

// 最小值
uint8_t MIN_SInt(uint8_t EN, SInt* out, int Count, ...) {
  if(!EN || !out || isnan(*out) || Count <= 0)
    return 0;

  va_list args;
  va_start(args, Count);

  SInt minVal = (SInt) va_arg(args, int); // SInt会提升为int传递
  for(int i = 1; i < Count; ++i) {
    SInt val = (SInt) va_arg(args, int); // 取出并强制转换
    if(val < minVal)
      minVal = val;
  }

  va_end(args);

  *out = minVal;
  return 1;
}
uint8_t MIN_Int(uint8_t EN, Int* out, int Count, ...) {
  if(!EN || !out || isnan(*out) || Count <= 0)
    return 0;
  va_list args;
  va_start(args, Count);
  Int minVal = (Int) va_arg(args, int);
  for(int i = 1; i < Count; ++i) {
    Int val = (Int) va_arg(args, int);
    if(val < minVal)
      minVal = val;
  }
  va_end(args);
  *out = minVal;
  return 1;
}
uint8_t MIN_DInt(uint8_t EN, DInt* out, int Count, ...) {
  if(!EN || !out || isnan(*out) || Count <= 0)
    return 0;
  va_list args;
  va_start(args, Count);
  DInt minVal = (DInt) va_arg(args, int32_t);
  for(int i = 1; i < Count; ++i) {
    DInt val = (DInt) va_arg(args, int32_t);
    if(val < minVal)
      minVal = val;
  }
  va_end(args);
  *out = minVal;
  return 1;
}
uint8_t MIN_USInt(uint8_t EN, USInt* out, int Count, ...) {
  if(!EN || !out || isnan(*out) || Count <= 0)
    return 0;
  va_list args;
  va_start(args, Count);
  USInt minVal = (USInt) va_arg(args, int); // USInt提升为int
  for(int i = 1; i < Count; ++i) {
    USInt val = (USInt) va_arg(args, int);
    if(val < minVal)
      minVal = val;
  }
  va_end(args);
  *out = minVal;
  return 1;
}
uint8_t MIN_UInt(uint8_t EN, UInt* out, int Count, ...) {
  if(!EN || !out || isnan(*out) || Count <= 0)
    return 0;
  va_list args;
  va_start(args, Count);
  UInt minVal = (UInt) va_arg(args, int); // UInt提升为int
  for(int i = 1; i < Count; ++i) {
    UInt val = (UInt) va_arg(args, int);
    if(val < minVal)
      minVal = val;
  }
  va_end(args);
  *out = minVal;
  return 1;
}
uint8_t MIN_UDInt(uint8_t EN, UDInt* out, int Count, ...) {
  if(!EN || !out || isnan(*out) || Count <= 0)
    return 0;
  va_list args;
  va_start(args, Count);
  UDInt minVal = (UDInt) va_arg(args, uint32_t);
  for(int i = 1; i < Count; ++i) {
    UDInt val = (UDInt) va_arg(args, uint32_t);
    if(val < minVal)
      minVal = val;
  }
  va_end(args);
  *out = minVal;
  return 1;
}
uint8_t MIN_Real(uint8_t EN, Real* out, int Count, ...) {
  if(!EN || !out || isnan(*out) || Count <= 0)
    return 0;
  va_list args;
  va_start(args, Count);
  Real minVal = (Real) va_arg(args, double); // float提升为double
  for(int i = 1; i < Count; ++i) {
    Real val = (Real) va_arg(args, double);
    if(val < minVal)
      minVal = val;
  }
  va_end(args);
  *out = minVal;
  return 1;
}
uint8_t MIN_LReal(uint8_t EN, LReal* out, int Count, ...) {
  if(!EN || !out || isnan(*out) || Count <= 0)
    return 0;
  va_list args;
  va_start(args, Count);
  LReal minVal = va_arg(args, double);
  for(int i = 1; i < Count; ++i) {
    LReal val = va_arg(args, double);
    if(val < minVal)
      minVal = val;
  }
  va_end(args);
  *out = minVal;
  return 1;
}
uint8_t MIN_DTL(uint8_t EN, DTL* out, int Count, ...) {
  if(!EN || !out || Count <= 0)
    return 0;
  va_list args;
  va_start(args, Count);
  DTL minVal = va_arg(args, DTL); // 直接传递结构体
  for(int i = 1; i < Count; ++i) {
    DTL val = va_arg(args, DTL);       // 直接传递结构体
    if(compare_DTL(&val, &minVal) < 0) // 使用比较函数
      minVal = val;
  }
  va_end(args);
  *out = minVal;
  return 1;
}

// 范围赋值
uint8_t LIMIT_SInt(uint8_t EN, SInt* out, SInt in, SInt min, SInt max) {
  if(!EN || !out || min > max || isnan(*out) || isnan(in) || isnan(min) || isnan(max))
    return 0;
  if(in < min)
    *out = min;
  else if(in > max)
    *out = max;
  else
    *out = in;
  return 1;
}
uint8_t LIMIT_Int(uint8_t EN, Int* out, Int in, Int min, Int max) {
  if(!EN || !out || min > max || isnan(*out) || isnan(in) || isnan(min) || isnan(max))
    return 0;
  if(in < min)
    *out = min;
  else if(in > max)
    *out = max;
  else
    *out = in;
  return 1;
}
uint8_t LIMIT_DInt(uint8_t EN, DInt* out, DInt in, DInt min, DInt max) {
  if(!EN || !out || min > max || isnan(*out) || isnan(in) || isnan(min) || isnan(max))
    return 0;
  if(in < min)
    *out = min;
  else if(in > max)
    *out = max;
  else
    *out = in;
  return 1;
}
uint8_t LIMIT_USInt(uint8_t EN, USInt* out, USInt in, USInt min, USInt max) {
  if(!EN || !out || min > max || isnan(*out) || isnan(in) || isnan(min) || isnan(max))
    return 0;
  if(in < min)
    *out = min;
  else if(in > max)
    *out = max;
  else
    *out = in;
  return 1;
}
uint8_t LIMIT_UInt(uint8_t EN, UInt* out, UInt in, UInt min, UInt max) {
  if(!EN || !out || min > max || isnan(*out) || isnan(in) || isnan(min) || isnan(max))
    return 0;
  if(in < min)
    *out = min;
  else if(in > max)
    *out = max;
  else
    *out = in;
  return 1;
}
uint8_t LIMIT_UDInt(uint8_t EN, UDInt* out, UDInt in, UDInt min, UDInt max) {
  if(!EN || !out || min > max || isnan(*out) || isnan(in) || isnan(min) || isnan(max))
    return 0;
  if(in < min)
    *out = min;
  else if(in > max)
    *out = max;
  else
    *out = in;
  return 1;
}
uint8_t LIMIT_Real(uint8_t EN, Real* out, Real in, Real min, Real max) {
  if(!EN || !out || min > max || isnan(*out) || isnan(in) || isnan(min) || isnan(max))
    return 0;
  if(in < min)
    *out = min;
  else if(in > max)
    *out = max;
  else
    *out = in;
  return 1;
}
uint8_t LIMIT_LReal(uint8_t EN, LReal* out, LReal in, LReal min, LReal max) {
  if(!EN || !out || min > max || isnan(*out) || isnan(in) || isnan(min) || isnan(max))
    return 0;
  if(in < min)
    *out = min;
  else if(in > max)
    *out = max;
  else
    *out = in;
  return 1;
}
uint8_t LIMIT_DTL(uint8_t EN, DTL* out, DTL* in, DTL* min, DTL* max) {
  if(!EN || !out || compare_DTL(min, max) > 0)
    return 0;
  if(compare_DTL(in, min) < 0)
    *out = *min; // 小于最小值
  else if(compare_DTL(in, max) > 0)
    *out = *max; // 大于最大值
  else
    *out = *in; // 在范围内
  return 1;
}

// 计算平方
uint8_t SQR_Real(uint8_t EN, Real* out, Real in) {
  if(!EN || !out || isnan(*out) || isnan(in))
    return 0;
  *out = in * in; // 计算平方
  return 1;
}
uint8_t SQR_LReal(uint8_t EN, LReal* out, LReal in) {
  if(!EN || !out || isnan(*out) || isnan(in))
    return 0;
  *out = (LReal) (in * in); // 计算平方
  return 1;
}

// 计算平方根
uint8_t SQRT_Real(uint8_t EN, Real* out, Real in) {
  if(!EN || !out || in < 0.0f || isnan(in) || isnan(*out))
    return 0;       // 不允许负数输入
  *out = sqrtf(in); // 使用 sqrtf 计算平方根
  return 1;
}
uint8_t SQRT_LReal(uint8_t EN, LReal* out, LReal in) {
  if(!EN || !out || in < 0.0 || isnan(in) || isnan(*out))
    return 0;      // 不允许负数输入
  *out = sqrt(in); // 使用 sqrt 计算平方根
  return 1;
}

uint8_t LN_Real(uint8_t EN, Real* out, Real in) {
  if(!EN || isnan(in) || in <= 0.0f || !out || isnan(*out))
    return 0; // ENO = 0

  *out = logf(in); // 计算自然对数
  return 1;        // ENO = 1
}

uint8_t LN_LReal(uint8_t EN, LReal* out, LReal in) {
  if(!EN || isnan(in) || in <= 0.0 || !out || isnan(*out))
    return 0;

  *out = log(in);
  return 1;
}
/**
 * @brief 计算自然对数 ln(x)，x 必须 > 0
 * @param x 输入值
 * @return 如果 x <= 0，则返回 0（也可以改为 NAN 或报错处理）
 */
double LN(double x) {
  double result = 0.0;
  LN_LReal(true, &result, x);
  return result;
}

/**
 * @brief 计算自然指数 e^x
 * @param x 输入值
 * @return 指数结果；
 */
float EXP(float x) {
  float result = 0.0f;
  EXP_Real(true, &result, x);
  return result;
}

// 指数计算：单精度 float
uint8_t EXP_Real(uint8_t EN, Real* out, Real in) {
  if(!EN || isnan(in) || !out || isnan(*out))
    return 0; // ENO = 0

  *out = expf(in); // 计算 e^in
  return 1;        // ENO = 1
}

// 指数计算：双精度 double
uint8_t EXP_LReal(uint8_t EN, LReal* out, LReal in) {
  if(!EN || isnan(in) || !out || isnan(*out))
    return 0;

  *out = exp(in); // 计算 e^in
  return 1;
}

// 计算正弦值（float）
Bool SIN_Real(Bool EN, Real* out, Real in) {
  if(!EN || isnan(in) || !out || isnan(*out))
    return 0; // ENO = 0

  *out = sinf(in); // 使用 sinf 计算单精度
  return 1;        // ENO = 1
}

// 计算正弦值（double）
Bool SIN_LReal(Bool EN, LReal* out, LReal in) {
  if(!EN || isnan(in) || !out || isnan(*out))
    return 0;

  *out = sin(in); // 使用 sin 计算双精度
  return 1;
}

// 计算余弦值（float）
Bool COS_Real(Bool EN, Real* out, Real in) {
  if(!EN || isnan(in) || !out || isnan(*out))
    return 0; // ENO = 0

  *out = cosf(in); // 单精度余弦
  return 1;        // ENO = 1
}

// 计算余弦值（double）
Bool COS_LReal(Bool EN, LReal* out, LReal in) {
  if(!EN || isnan(in) || !out || isnan(*out))
    return 0;

  *out = cos(in); // 双精度余弦
  return 1;
}

// 计算正切值（float）
Bool TAN_Real(Bool EN, Real* out, Real in) {
  if(!EN || isnan(in) || !out || isnan(*out))
    return 0; // ENO = 0

  *out = tanf(in); // 单精度正切
  return 1;        // ENO = 1
}

// 计算正切值（double）
Bool TAN_LReal(Bool EN, LReal* out, LReal in) {
  if(!EN || isnan(in) || !out || isnan(*out))
    return 0;

  *out = tan(in); // 双精度正切
  return 1;
}

// 计算反正弦（float）
Bool ASIN_Real(Bool EN, Real* out, Real in) {
  if(!EN || isnan(in) || !out || in < -1.0f || in > 1.0f || isnan(*out))
    return 0; // ENO = 0

  *out = asinf(in); // 单精度反正弦（结果为弧度）
  return 1;         // ENO = 1
}

// 计算反正弦（double）
Bool ASIN_LReal(Bool EN, LReal* out, LReal in) {
  if(!EN || isnan(in) || !out || in < -1.0 || in > 1.0 || isnan(*out))
    return 0;

  *out = asin(in); // 双精度反正弦
  return 1;
}

// 计算反余弦值（float）
Bool ACOS_Real(Bool EN, Real* out, Real in) {
  if(!EN || isnan(in) || !out || in < -1.0f || in > 1.0f || isnan(*out))
    return 0; // ENO = 0

  *out = acosf(in); // 单精度反余弦（结果为弧度，0 ~ π）
  return 1;         // ENO = 1
}

// 计算反余弦值（double）
Bool ACOS_LReal(Bool EN, LReal* out, LReal in) {
  if(!EN || isnan(in) || !out || in < -1.0 || in > 1.0 || isnan(*out))
    return 0;

  *out = acos(in); // 双精度反余弦
  return 1;
}

// 计算反正切值（float）
Bool ATAN_Real(Bool EN, Real* out, Real in) {
  if(!EN || isnan(in) || !out || isnan(*out))
    return 0; // ENO = 0

  *out = atanf(in); // 单精度反正切（弧度）
  return 1;         // ENO = 1
}

// 计算反正切值（double）
Bool ATAN_LReal(Bool EN, LReal* out, LReal in) {
  if(!EN || isnan(in) || !out || isnan(*out))
    return 0;

  *out = atan(in); // 双精度反正切（弧度）
  return 1;
}

// 提取小数部分（float）
Bool FRAC_Real(Bool EN, Real* out, Real in) {
  if(!EN || isnan(in) || !out || isnan(*out))
    return 0; // ENO = 0
  if(in > 0)
    *out = in - floorf(in); // 获取小数部分（保留非负）
  else {
    *out = -(-in - floorf(-in));
  }
  return 1; // ENO = 1
}

// 提取小数部分（double）
Bool FRAC_LReal(Bool EN, LReal* out, LReal in) {
  if(!EN || isnan(in) || !out || isnan(*out))
    return 0;
  if(in > 0)
    *out = in - floor(in);
  else {
    *out = -(-in - floor(-in));
  }
  return 1;
}

Bool EXPT_Real(Bool EN, Real* out, Real in1, Real in2) {
  if(!EN || !out || isnan(in1) || isnan(in2) || isnan(*out))
    return 0;

  Real result = powf(in1, in2);
  if(isnan(result) || isinf(result))
    return 0; // 出错，返回 ENO=0

  *out = result;
  return 1;
}

Bool EXPT_LReal(Bool EN, LReal* out, LReal in1, LReal in2) {
  if(!EN || !out || isnan(in1) || isnan(in2) || isnan(*out))
    return 0;

  LReal result = pow(in1, in2);
  if(isnan(result) || isinf(result))
    return 0;

  *out = result;
  return 1;
}
